/*
 * Copyright (c) 2003
 * XDoclet Team
 * All rights reserved.
 */
package org.xdoclet.plugin.qtags.impl;

import org.generama.QDoxCapableMetadataProvider;
import org.generama.VelocityTemplateEngine;
import org.generama.WriterMapper;

import org.generama.defaults.QDoxPlugin;

import org.xdoclet.plugin.qtags.QTagUtils;

import com.thoughtworks.qdox.model.JavaClass;

/**
 * Creates a library registering qtags for plugin.
 * @author Aslak Helles&oslash;y
 * @version $Revision: 525 $
 */
public class QTagLibraryPlugin extends QDoxPlugin {
    private QTagUtils qTagUtils = new QTagUtils();

    public QTagLibraryPlugin(VelocityTemplateEngine velocityTemplateEngine,
        QDoxCapableMetadataProvider metadataProvider, WriterMapper writerMapper) {
        super(velocityTemplateEngine, metadataProvider, writerMapper);
        setMultioutput(false);
        setFilereplace("TagLibrary.java");
        setPackagereplace("");
        qTagUtils.registerLibrary(metadataProvider);
    }

    public QTagUtils getQTagUtils() {
        return qTagUtils;
    }

    public boolean shouldGenerate(Object metadata) {
        return qTagUtils.shouldGenerate(metadata);
    }
    
    public String getConstName(JavaClass clazz) {
        char[] tagName = qTagUtils.getDocletTagName(clazz).toCharArray();
        StringBuffer retBuf = new StringBuffer();
        for (int i = 0; i < tagName.length; i++) {
            char ch = tagName[i];
            if ((i == 0) && Character.isJavaIdentifierStart(ch)) {
                retBuf.append(Character.toTitleCase(ch));
            } else if ((i > 0) && Character.isJavaIdentifierPart(ch)) {
                retBuf.append(Character.toTitleCase(ch));
            } else {
                retBuf.append('_');
            }
        }
        return retBuf.toString();
    }
}